package com.example.springdata.tools;

import java.io.*;

public class FileEncodeAndDecode {
    private static final int ENCANDDECPASSWORD=0x99;
    private static int dataOfFile=0;
    public static void encodeFile(File srcFile, File encFile)throws Exception{
        if(!srcFile.exists()){
            return;
        }
        if(!encFile.exists()){
            encFile.createNewFile();
        }
        InputStream fis=new FileInputStream(srcFile);
        OutputStream fos=new FileOutputStream(encFile);
        while((dataOfFile=fis.read())>-1){
            fos.write(dataOfFile^ENCANDDECPASSWORD);
        }
        fis.close();
        fos.flush();
        fos.close();

    }
    public static void decFile(File encFile,File decFile)throws  Exception{
        if(!encFile.exists()){
            return;
        }
        if(!decFile.exists()){
            decFile.createNewFile();
        }
        InputStream fis=new FileInputStream(encFile);
        OutputStream fos=new FileOutputStream(decFile);
        while((dataOfFile=fis.read())>-1){
            fos.write(dataOfFile^ENCANDDECPASSWORD);
        }
        fis.close();
        fos.flush();
        fos.close();
    }

    public static void main(String[] args) {
        File srcFile=new File("D:/home/data/file/src.txt");
        File decFile=new File("D:/home/data/file/desrc.txt");
        try {
            FileEncodeAndDecode.encodeFile(srcFile,decFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        File encFile=new File("D:/home/data/file/ensrc.txt");
        try{
            FileEncodeAndDecode.encodeFile(decFile,encFile);
        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
